<template>
  <div>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" :clearable="true" @clear="clearCallBack"
                    @keyup.enter.native="getOrderList">
            <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <el-table :data="ordersList" stripe style="width: 100%" border>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格"></el-table-column>
        <el-table-column label="是否付款">
          <template v-slot="scopeInfo">
            <el-tag type="danger" v-if="scopeInfo.row.pay_status==='0'">未付款</el-tag>
            <el-tag type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"></el-table-column>
        <el-table-column label="下单时间">
          <template v-slot="scopeInfo">
            {{ scopeInfo.row.create_time | format }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template v-slot="scopeInfo">
            <!-- 编辑 -->
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showAddress()"></el-button>
            <!-- 物流进度 -->
            <el-tooltip class="item" effect="dark" content="物流进度" placement="top" :enterable="false">
              <el-button type="success" icon="el-icon-location" size="mini" @click="showLogistics"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5,10,30,50]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>

    </el-card>

    <!-- 编辑地址 -->
    <el-dialog
      title="提示"
      :visible.sync="addressDialogVisible"
      width="50%"
      @closed="handleClose">
      <el-form ref="addressFormRef" :model="addressForm" label-width="80px">
        <el-form-item label="省/市/区" prop="citySelect">
          <el-cascader
            v-model="addressForm.citySelect"
            :options="city"
            :props="{ expandTrigger: 'hover' }"
            style="width: 100%;"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="addressInfo">
          <el-input v-model="addressForm.addressInfo"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="addressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addressDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      title="物流进度"
      :visible.sync="logisticsDialogVisible"
      width="50%">
      <el-timeline>
        <el-timeline-item
          v-for="(item, index) in logisticsInfo"
          :key="index"
          :timestamp="item.time">
          {{item.context}}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import { apiGetOrdersList, apiGetLogistic } from '@/api/index.js'
import cityData from './citydata'

export default {
  name: 'Order',
  created () {
    this.getOrderList()
  },
  data () {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      ordersList: [],
      // 修改地址
      addressDialogVisible: false,
      city: cityData,
      addressForm: {
        citySelect: '',
        addressInfo: ''
      },
      // 物流进度
      logisticsDialogVisible: false,
      logisticsInfo: []
    }
  },
  methods: {
    // 获取订单列表
    async getOrderList () {
      const { data: res } = await apiGetOrdersList(this.queryInfo)
      if (res.meta.status !== 200) {
        return this.$message.error(`${res.meta.status}: ${res.meta.msg}`)
      }
      this.ordersList = res.data.goods
      this.total = res.data.total
    },
    clearCallBack () {
      this.getOrderList()
    },
    handleSizeChange (val) {
      this.queryInfo.pagesize = val
      this.getOrderList()
    },
    handleCurrentChange (val) {
      this.queryInfo.pagenum = val
      this.getOrderList()
    },
    // TODO 编辑，查看物流状态
    // 编辑地址
    showAddress () {
      this.addressDialogVisible = true
    },
    handleClose () {
      this.$refs.addressFormRef.resetFields()
    },
    // 物流进度
    async showLogistics () {
      const { data: res } = await apiGetLogistic('1106975712662')
      if (res.meta.status !== 200) {
        return this.$message.error(`${res.meta.status}: ${res.meta.msg}`)
      }
      this.logisticsInfo = res.data
      this.logisticsDialogVisible = true
    }
  }
}
</script>

<style scoped>

</style>
